package com.ict.ais.abnormal.service.api.impl;

import com.ict.ais.abnormal.facade.api.IAbnormalService;
import com.ict.ais.abnormal.facade.dto.AreaSet;
import com.ict.ais.abnormal.facade.dto.ResultObj;
import com.ict.ais.abnormal.facade.model.AISOffLine;
import com.ict.ais.abnormal.facade.model.AbnormalSet;
import com.ict.ais.abnormal.facade.model.AnomalyGrids;
import com.ict.ais.abnormal.facade.model.AnomalyShips;
import com.ict.ais.abnormal.facade.model.ImTarget;
import com.ict.ais.abnormal.facade.model.ZoneEntry;
import com.ict.ais.abnormal.facade.model.ZoneEntryCache;
import com.ict.ais.abnormal.service.dao.IAbnormalDao;
import com.ict.ais.abnormal.service.rabbitmq.MQTopicPublish_Abnormal;
import com.ict.ais.abnormal.service.rabbitmq.ProperUtils_Abnormal;
import com.ict.ais.area.facade.model.Point;

import common.SplitUtils;
import mqutils.MQTopicPublish;
import mqutils.ProperUtilsMQ;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service("abnormalService")
public class AbnormalService implements IAbnormalService   {
    @Resource
    IAbnormalDao iAbnormalDao;

    /**
     * 区域异常设置添加-矩形
     * @param areaSet
     * @return
     */
    public boolean areaAbnormalSetRectangle(AreaSet areaSet) {
        team.iOceanPlus.PB.Config.PBConfig.Builder configPB =
                team.iOceanPlus.PB.Config.PBConfig.newBuilder();
        team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.Builder pbConfigZonesOfInterest
                = team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.newBuilder();

        team.iOceanPlus.PB.Regions.PBRegion.Builder pbRegion =
                team.iOceanPlus.PB.Regions.PBRegion.newBuilder();

        team.iOceanPlus.PB.Regions.PBRegion_Rectangle.Builder pBRegion_Rectangle =
                team.iOceanPlus.PB.Regions.PBRegion_Rectangle.newBuilder();
        team.iOceanPlus.PB.Regions.PBGeoPoint.Builder lowerLeftPoint =
                team.iOceanPlus.PB.Regions.PBGeoPoint.newBuilder();
        team.iOceanPlus.PB.Regions.PBGeoPoint.Builder upperRightPoint =
                team.iOceanPlus.PB.Regions.PBGeoPoint.newBuilder();
        lowerLeftPoint.setIntLongitudeX60W((int) (areaSet.getLdlon() * 600000));
        lowerLeftPoint.setIntLatitudeX60W((int) (areaSet.getLdlat() * 600000));
        upperRightPoint.setIntLongitudeX60W((int) (areaSet.getRulon() * 600000));
        upperRightPoint.setIntLatitudeX60W((int) (areaSet.getRulat() * 600000));
        pBRegion_Rectangle.setLowerLeftPoint(lowerLeftPoint);
        pBRegion_Rectangle.setUpperRightPoint(upperRightPoint);
        pbRegion.setRectangle(pBRegion_Rectangle);

        pbRegion.setRegionName(areaSet.getAreaName());
        pbRegion.setRegionID(areaSet.getAreaId());
        pbConfigZonesOfInterest.setEnumConfigType
                (team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Add);//设置配置类型为添加
        pbConfigZonesOfInterest.setRegion(pbRegion);

        configPB.addListConfigZonesOfInterest(pbConfigZonesOfInterest);

        //        连接RabbitMQ将PB格式数据转成byte[]发送到指定的exchange
        ProperUtilsMQ properUtils = new ProperUtilsMQ
                ("xgs","Configuration.Alert.ZoneOfInterest","");
        MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtils.getHost(),
                properUtils.getUser(), properUtils.getPassWord(),
                properUtils.getExchange());

        if (mqTopicPublish.BaseConnection(properUtils.getPort()) != 0) {
            System.out.println("连接RabbiqMQ失败");
            return false;
        }

        team.iOceanPlus.PB.Config.PBConfig bui = configPB.build();

        if (bui!=null){
            byte[] result = bui.toByteArray();
            if (!mqTopicPublish.Publish(properUtils.getRoutingKey(), result)) {
                System.out.println("发送RabbitMQ消息失败");
                return false;
            }
            return true;
        }else {
            return false;
        }
    }

    /**
     * 区域异常设置添加-多边形
     * @param points
     * @param areaName
     * @return
     */
    public boolean areaAbnormalSetPolygons(Point[] points,String areaName,int areaId){
        team.iOceanPlus.PB.Config.PBConfig.Builder configPB =
                team.iOceanPlus.PB.Config.PBConfig.newBuilder();
        team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.Builder pbConfigZonesOfInterest
                = team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.newBuilder();

        team.iOceanPlus.PB.Regions.PBRegion.Builder pbRegion =
                team.iOceanPlus.PB.Regions.PBRegion.newBuilder();
        pbRegion.setRegionName(areaName);
        pbRegion.setRegionID(areaId);

        team.iOceanPlus.PB.Regions.PBRegion_Polygon.Builder
                pbRegion_Polygon=team.iOceanPlus.PB.Regions.PBRegion_Polygon.newBuilder();
        for(int i=0;i<points.length;i++){
            team.iOceanPlus.PB.Regions.PBGeoPoint.Builder
                    pbGeoPoint=team.iOceanPlus.PB.Regions.PBGeoPoint.newBuilder();
            pbGeoPoint.setIntLongitudeX60W(points[i].getLon());
            pbGeoPoint.setIntLatitudeX60W(points[i].getLat());
            pbRegion_Polygon.addListPoints(pbGeoPoint);
        }

        pbRegion.setPolygon(pbRegion_Polygon);
        pbConfigZonesOfInterest.setRegion(pbRegion);
        pbConfigZonesOfInterest.setEnumConfigType
                (team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Add);//设置配置类型为添加

        configPB.addListConfigZonesOfInterest(pbConfigZonesOfInterest);

        //        连接RabbitMQ将PB格式数据转成byte[]发送到指定的exchange
        ProperUtilsMQ properUtils = new ProperUtilsMQ
                ("xgs","Configuration.Alert.ZoneOfInterest","");
        MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtils.getHost(),
                properUtils.getUser(), properUtils.getPassWord(),
                properUtils.getExchange());

        if (mqTopicPublish.BaseConnection(properUtils.getPort()) != 0) {
            System.out.println("连接RabbiqMQ失败");
            return false;
        }

        team.iOceanPlus.PB.Config.PBConfig bui = configPB.build();
        byte[] result = bui.toByteArray();

        if (!mqTopicPublish.Publish(properUtils.getRoutingKey(), result)) {
            System.out.println("发送RabbitMQ消息失败");
            return false;
        }
        return true;
    }

    /**
     * 区域异常设置添加-圆形
     * @param point
     * @param radius
     * @param areaName
     * @return
     */
    public boolean areaAbnormalSetCircle(Point point,int radius,String areaName,int areaId){
        team.iOceanPlus.PB.Config.PBConfig.Builder configPB =
                team.iOceanPlus.PB.Config.PBConfig.newBuilder();
        team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.Builder pbConfigZonesOfInterest
                = team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.newBuilder();

        team.iOceanPlus.PB.Regions.PBRegion.Builder pbRegion =
                team.iOceanPlus.PB.Regions.PBRegion.newBuilder();
        pbRegion.setRegionName(areaName);
        pbRegion.setRegionID(areaId);

        team.iOceanPlus.PB.Regions.PBRegion_Circle.Builder
                pbRegion_Circle=team.iOceanPlus.PB.Regions.PBRegion_Circle.newBuilder();

        team.iOceanPlus.PB.Regions.PBGeoPoint.Builder
                pbGeoPoint=team.iOceanPlus.PB.Regions.PBGeoPoint.newBuilder();
        pbGeoPoint.setIntLongitudeX60W(point.getLon());
        pbGeoPoint.setIntLatitudeX60W(point.getLat());

        pbRegion_Circle.setCentre(pbGeoPoint);
        pbRegion_Circle.setRadiusInKM((float)( radius / 1000));

        pbRegion.setCircle(pbRegion_Circle);
        pbConfigZonesOfInterest.setRegion(pbRegion);
        pbConfigZonesOfInterest.setEnumConfigType
                (team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Add);//设置配置类型为添加
        configPB.addListConfigZonesOfInterest(pbConfigZonesOfInterest);

        //        连接RabbitMQ将PB格式数据转成byte[]发送到指定的exchange
        ProperUtilsMQ properUtils = new ProperUtilsMQ
                ("xgs","Configuration.Alert.ZoneOfInterest","");
        MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtils.getHost(),
                properUtils.getUser(), properUtils.getPassWord(),
                properUtils.getExchange());

        if (mqTopicPublish.BaseConnection(properUtils.getPort()) != 0) {
            System.out.println("连接RabbiqMQ失败");
            return false;
        }

        try{
            team.iOceanPlus.PB.Config.PBConfig bui = configPB.build();
            byte[] result = bui.toByteArray();

            if (!mqTopicPublish.Publish(properUtils.getRoutingKey(), result)) {
                System.out.println("发送RabbitMQ消息失败");
                return false;
            }
            return true;
        }catch (Exception e2){
            System.out.println("空指针---:"+e2);
            return false;
        }

    }

    /**
     * 区域异常设置添加-扇形，扇环形
     * @param point
     * @param radius
     * @param areaName
     * @return
     */
    public boolean areaAbnormalSetSector(Point point,int radius,int radsmal,double start_deg,double end_deg,String areaName,int areaId){
        team.iOceanPlus.PB.Config.PBConfig.Builder configPB =
                team.iOceanPlus.PB.Config.PBConfig.newBuilder();
        team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.Builder pbConfigZonesOfInterest
                = team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.newBuilder();

        team.iOceanPlus.PB.Regions.PBRegion.Builder pbRegion =
                team.iOceanPlus.PB.Regions.PBRegion.newBuilder();
        pbRegion.setRegionName(areaName);
        pbRegion.setRegionID(areaId);

        //创建圆形
        team.iOceanPlus.PB.Regions.PBRegion_Circle.Builder
                pbRegion_Circle=team.iOceanPlus.PB.Regions.PBRegion_Circle.newBuilder();

        //创建扇形
        team.iOceanPlus.PB.Regions.PBRegion_CircularSector.Builder
                pbRegion_circularSector=team.iOceanPlus.PB.Regions.PBRegion_CircularSector.newBuilder();

        //创建扇环形
        team.iOceanPlus.PB.Regions.PBRegion_AnnularSector.Builder
                pbRegion_annularSector=team.iOceanPlus.PB.Regions.PBRegion_AnnularSector.newBuilder();

        //创建点
        team.iOceanPlus.PB.Regions.PBGeoPoint.Builder
                pbGeoPoint=team.iOceanPlus.PB.Regions.PBGeoPoint.newBuilder();
        pbGeoPoint.setIntLongitudeX60W(point.getLon());
        pbGeoPoint.setIntLatitudeX60W(point.getLat());

        //用点组成圆的元素
        pbRegion_Circle.setCentre(pbGeoPoint);
        pbRegion_Circle.setRadiusInKM((float) (radius / 1000));

        //参数设置到扇形
        //pbRegion.setCircle(pbRegion_Circle);
        pbRegion_circularSector.setCircle(pbRegion_Circle);
        pbRegion_circularSector.setStartDirectionInDegree(start_deg);
        pbRegion_circularSector.setEndDirectionInDegree(end_deg);

        //判断是否为扇环形
        if(radsmal==0){
            //扇形操作
            pbRegion.setCircularSector(pbRegion_circularSector);
        }else {
            //参数设置到扇环形
            pbRegion_annularSector.setCircularSector(pbRegion_circularSector);
            pbRegion_annularSector.setRadiusInnerInKM((float)( radsmal / 1000));
            //扇环形操作
            pbRegion.setAnnularSector(pbRegion_annularSector);
        }

        //设置异常区域
        pbConfigZonesOfInterest.setRegion(pbRegion);
        pbConfigZonesOfInterest.setEnumConfigType
                (team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Add);//设置配置类型为添加
        configPB.addListConfigZonesOfInterest(pbConfigZonesOfInterest);

        //        连接RabbitMQ将PB格式数据转成byte[]发送到指定的exchange
        ProperUtilsMQ properUtils = new ProperUtilsMQ
                ("xgs","Configuration.Alert.ZoneOfInterest","");
        MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtils.getHost(),
                properUtils.getUser(), properUtils.getPassWord(),
                properUtils.getExchange());

        if (mqTopicPublish.BaseConnection(properUtils.getPort()) != 0) {
            System.out.println("连接RabbiqMQ失败");
            return false;
        }

        team.iOceanPlus.PB.Config.PBConfig bui = configPB.build();
        byte[] result = bui.toByteArray();

        if (!mqTopicPublish.Publish(properUtils.getRoutingKey(), result)) {
            System.out.println("发送RabbitMQ消息失败");
            return false;
        }
        return true;
    }

    /**
     * 区域异常设置删除
     *
     * @param areaIdArr
     * @return
     */
    public boolean areaSetDelete(String areaIdArr) {
        try {
            team.iOceanPlus.PB.Config.PBConfig.Builder configPB =
                    team.iOceanPlus.PB.Config.PBConfig.newBuilder();

            String[] StrArray = SplitUtils.MDVNumSplit_StrArray(areaIdArr);
            for (int i = 0; i < StrArray.length; i++) {
                team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.Builder
                        pbConfigZonesOfInterest = team.iOceanPlus.PB.Config.PBConfigZonesOfInterest.newBuilder();
                team.iOceanPlus.PB.Regions.PBRegion.Builder pbRegion =
                        team.iOceanPlus.PB.Regions.PBRegion.newBuilder();
                pbConfigZonesOfInterest.setEnumConfigType
                        (team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Delete);

                pbRegion.setRegionID(Integer.parseInt(StrArray[i]));
                pbConfigZonesOfInterest.setRegion(pbRegion);
                configPB.addListConfigZonesOfInterest(pbConfigZonesOfInterest);
            }

//        连接RabbitMQ将PB格式数据转成byte[]发送到指定的exchange
            ProperUtilsMQ properUtils = new ProperUtilsMQ
                    ("xgs","Configuration.Alert.ZoneOfInterest","");
            MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtils.getHost(),
                    properUtils.getUser(), properUtils.getPassWord(),
                    properUtils.getExchange());

            if (mqTopicPublish.BaseConnection(properUtils.getPort()) != 0) {
                System.out.println("连接RabbiqMQ失败");
                return false;
            }

            team.iOceanPlus.PB.Config.PBConfig bui = configPB.build();
            byte[] result = bui.toByteArray();

            if (!mqTopicPublish.Publish(properUtils.getRoutingKey(), result)) {
                System.out.println("发送RabbitMQ消息失败");
                return false;
            }
            return true;
        } catch (Exception e) {
            System.out.println("操作异常" + e);
            return false;
        }
    }

    /**
     * 异常设置
     *
     * @param abnormalSet
     * @return
     */
    public boolean otherAbnormalSet(AbnormalSet abnormalSet) {
        team.iOceanPlus.PB.Config.PBConfig.Builder configPB =
                team.iOceanPlus.PB.Config.PBConfig.newBuilder();

        team.iOceanPlus.PB.Config.PBConfigAlertAnomaly.Builder configAlertAnomaly =
                team.iOceanPlus.PB.Config.PBConfigAlertAnomaly.newBuilder();
        configAlertAnomaly.setSecondsThreshAISOffLine(abnormalSet.getClosetime());

        configAlertAnomaly.setTimesOfAvgThreshGridAnomalyTooMuchShips(abnormalSet.getCou
                ());

        configAlertAnomaly.setSOGKnotsX10CargoShipHigherThresh(abnormalSet.getTp1());
        configAlertAnomaly.setSOGKnotsX10MilitaryShipHigherThresh(abnormalSet.getTp2());
        configAlertAnomaly.setSOGKnotsX10TankerHigherThresh(abnormalSet.getTp3());
        configAlertAnomaly.setSOGKnotsX10TugHigherThresh(abnormalSet.getTp4());
        configAlertAnomaly.setSOGKnotsX10FishingHigherThresh(abnormalSet.getTp5());
        configAlertAnomaly.setSOGKnotsX10TowingShipHigherThresh(abnormalSet.getTp6());
        configAlertAnomaly.setSOGKnotsX10PassengerShipHigherThresh(abnormalSet.getTp7());
        //--fcc修改部分
        configAlertAnomaly.setSOGKnotsX10MilitaryShipHigherThresh(abnormalSet.getTp8());
        configAlertAnomaly.setSOGKnotsX10LawEnforcementShipHigherThresh
                (abnormalSet.getTp9());
        configAlertAnomaly.setSOGKnotsX10WIGShipHigherThresh(abnormalSet.getTp10());
        configAlertAnomaly.setSOGKnotsX10HSCShipHigherThresh(abnormalSet.getTp11());
        configAlertAnomaly.setSOGKnotsX10OtherShipHigherThresh(abnormalSet.getDefa());
        configPB.setConfigAlertAnomaly(configAlertAnomaly);


//        连接RabbitMQ将PB格式数据转成byte[]发送到指定的exchange
        ProperUtils_Abnormal properUtils = new ProperUtils_Abnormal();
        MQTopicPublish_Abnormal mqTopicPublish = new MQTopicPublish_Abnormal
                (properUtils.getHost(), properUtils.getUser(), properUtils.getPassWord(),
                        properUtils.getExchange());

        if (mqTopicPublish.BaseConnection(properUtils.getPort()) != 0) {
            System.out.println("连接RabbiqMQ失败");
            return false;
        }

        team.iOceanPlus.PB.Config.PBConfig bui = configPB.build();
        byte[] result = bui.toByteArray();

        if (!mqTopicPublish.Publish(properUtils.getRoutingKey(), result)) {
            System.out.println("发送RabbitMQ消息失败");
            return false;
        }
        return true;
    }

    /**
     * 异常设置的获取
     *
     * @return
     */
    public AbnormalSet getAbnormalSet() {
        try {
            return iAbnormalDao.getAbnormalSet();
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     * 异常设置的更新
     *
     * @param abnormalSet
     * @return
     */
    public boolean updateAbnormalSet(AbnormalSet abnormalSet) {
        try {
            iAbnormalDao.updateAbnormalSet(abnormalSet);
            return true;
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return false;
        }
    }

    /**
     * 获取驶入敏感地带异常
     *
     * @return
     */
    public List<ZoneEntry> getZoneEntry(int tim,int limit) {
        try {
            if(limit==0){
                return iAbnormalDao.getZoneEntry(tim);
            }else if(limit>0){
                return iAbnormalDao.getZoneEntryPro(tim,limit);
            }else{
                return null;
            }
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     * 获取驶入敏感缓冲地带异常
     * @param tim
     * @param limit
     * @return
     */
    public List<ZoneEntryCache> getZoneEntryCache(int tim,int limit) {
        try {
            for(ZoneEntryCache cache:iAbnormalDao.getZoneEntryCache(tim,limit)){
                System.out.println(cache.getArea_name());
            }
            //System.out.println(iAbnormalDao.getZoneEntryCache(tim,limit).size());
            return iAbnormalDao.getZoneEntryCache(tim,limit);
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     * 获取船速过快异常
     *
     * @return
     */

    public List<AnomalyShips> getAnomalyShips(int tim,int limit) {
        try {
            if(limit==0){
                return iAbnormalDao.getAnomalyShips(tim);
            }else if(limit>0){
                return iAbnormalDao.getAnomalyShipsPro(tim, limit);
            }else{
                return null;
            }
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     * 疑似JS目标异常获取
     * @param tim
     * @param limit
     * @return
     */
    public List<AnomalyShips> getAnomalyJS(int tim,int limit) {
        try {
            if(limit==0){
                return iAbnormalDao.getAnomalyJS(tim);
            }else if(limit>0){
                return iAbnormalDao.getAnomalyJSLimit(tim, limit);
            }else{
                return null;
            }
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     * 疑似执法目标异常获取
     * @param tim
     * @param limit
     * @return
     */
    public List<AnomalyShips> getAnomalyZF(int tim,int limit) {
        try {
            if(limit==0){
                return iAbnormalDao.getAnomalyZF(tim);
            }else if(limit>0){
                return iAbnormalDao.getAnomalyZFLimit(tim, limit);
            }else{
                return null;
            }
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     * 获取异常聚集异常
     *
     * @return
     */
    public List<AnomalyGrids> getAnomalyGrids(int tim,int limit) {
        try {
            if(limit==0){
                return iAbnormalDao.getAnomalyGrids(tim);
            }else if(limit>0){
                return iAbnormalDao.getAnomalyGridsPro(tim, limit);
            }else{
                return null;
            }
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     * 获取AIS关闭异常
     *
     * @return
     */
    public List<AISOffLine> getAISOffLine(int tim,int limit) {
        try {
            if(limit==0){
                return iAbnormalDao.getAISOffLine(tim);
            }else if(limit>0){
                return iAbnormalDao.getAISOffLinePro(tim,limit);
            }else{
                return null;
            }
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            return null;
        }
    }

    /**
     *
     * @param tim
     * @param limit
     * @return
     */
    public ResultObj getImTarget(int tim,int limit){
        ResultObj resultObj=new ResultObj();
        List<ImTarget> imTargetList;
        try {
            if(limit==0){
                imTargetList=iAbnormalDao.getImTargetWithoutLimit(tim);
            }else if(limit>0){
                imTargetList=iAbnormalDao.getImTargetWithLimit(tim,limit);
            }else{
                resultObj.setState(-1);
                return resultObj;
            }

            if (imTargetList.size()<1){
                resultObj.setState(0);
                return resultObj;
            }else{
                resultObj.setState(1);
                resultObj.setObj(imTargetList);
                return resultObj;
            }
        } catch (Exception e) {
            System.out.println("数据操作失败" + e);
            resultObj.setState(-1);
            return resultObj;
        }
    }

	
}